﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.DAL.Dao;
using Telegnosis.Framework.Constants;
using Telegnosis.Framework.DataAccess.DB;

namespace CharacterTrailLibrary.Impl
{
    public class RepairImpl : IRepair
    {

        public Persist.BLL.Model.TRepairHeaderModel GetRepairHeaderModel(string barCode)
        {
            var sqlWhere = new SqlItem();
            string condition = " ProductItemBarCode = '{0}'";
            condition = string.Format(condition, barCode);
            sqlWhere.Text = condition;
            var repairHeaderDao = new TRepairHeaderDao();
            var list = repairHeaderDao.GetList(sqlWhere);
            if (list.Count == 0)
                return null;
            return list[0];
        }

        public void SaveRepairHeaderModel(Persist.BLL.Model.TRepairHeaderModel repairHeaderModel)
        {
            var repairHeaderDao = new TRepairHeaderDao();
            if(repairHeaderModel.EditFlag == Enums.EditFlag.New)
            {
                repairHeaderDao.InsertItem(repairHeaderModel.CreateOrm());
            }
            else if (repairHeaderModel.EditFlag == Enums.EditFlag.Update)
            {
                repairHeaderDao.UpdateItem(repairHeaderModel.CreateOrm());
            }
            else if (repairHeaderModel.EditFlag == Enums.EditFlag.Delete)
            {
                repairHeaderDao.DeleteItem(repairHeaderModel.CreateOrm());
            }
        }


        public List<Persist.BLL.Model.VRepairBaseModel> GetVRepairBaseModelsByCondition(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var vRepairBaseDao = new VRepairBaseDao();
            var list = vRepairBaseDao.GetList(sqlWhere);
            return list;
        }


        public List<Persist.BLL.Model.TRepairHeaderModel> GetRepairHeaderModelsByCondition(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var repairHeaderDao = new TRepairHeaderDao();
            return repairHeaderDao.GetList(sqlWhere);
        }
    }
}
